import { createAsyncThunk, createSlice, createEntityAdapter } from "@reduxjs/toolkit";
import { client } from "../../api/client";

//const initialState = []

export const fetchAuthors = createAsyncThunk("users/fetchUsers", async () => {
  const response = await client.get('/fakeApi/users')
  return response.data
})

const authorAdapter = createEntityAdapter()
const initialState = authorAdapter.getInitialState()

const authorsSlice = createSlice({
  name: "authors",
  initialState,
  reducers: {},
  extraReducers(builder) {
    builder.addCase(fetchAuthors.fulfilled, authorAdapter.setAll)
  }
})

export default authorsSlice.reducer;

//export const selectAllAuthors = state => state.authors
//export const selectAuthorById = (state, authorId) => state.authors.find(author => author.id === authorId)

export const {
  selectAll: selectAllAuthors,
  selectById: selectAuthorById
} = authorAdapter.getSelectors(state => state.authors)